package com.zhengjiang.controller;

import com.zhengjiang.entity.User;
import com.zhengjiang.repository.UserRepository;
import com.zhengjiang.vo.UserVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/user")
public class UserHandler {

    @Resource
    UserRepository userRepository;

    @GetMapping("/findAll/{page}/{limit}")
    public UserVO findAll(@PathVariable("page") int page, @PathVariable("limit") int limit){
        UserVO userVO = UserVO.builder()
                .code(0)
                .msg("").count(userRepository.count())
                .data(userRepository.findAll((page - 1) * limit, limit))
                .build();
        return userVO;
    }

    @PostMapping("/save")
    public void save(@RequestBody User user){
        user.setRegisterdate(new Date());
        userRepository.save(user);
    }

    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") long id){
        userRepository.deleteById(id);
    }
}
